/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-6-1 下午4:16 Created by lyyang
 */
package com.xunton.bss.weixin.api.coupon;

import java.io.File;
import java.util.HashMap;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.framework.base.util.HttpUtils;
import com.xunton.bss.weixin.api.Response;
import com.xunton.bss.weixin.api.coupon.pdu.Coupon;
import com.xunton.bss.weixin.api.coupon.pdu.StockQuantity;
import com.xunton.bss.weixin.api.coupon.response.CreateCouponResponse;
import com.xunton.bss.weixin.api.coupon.response.UploadLogoResponse;
import com.xunton.bss.weixin.api.token.TokenAgent;

/**
 * 卡卷接口
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-6-1 下午4:16
 */
@Named ("weixin_couponAgent")
public class CouponAgent
{
    private TokenAgent tokenAgent;

    @Inject
    public void setTokenAgent(@Named ("weixin_tokenAgent") TokenAgent tokenAgent)
    {
        this.tokenAgent = tokenAgent;
    }

    public static final String URL_UPLOAD_LOGO = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=";

    public static final String URL_CREATE_COUPON = "https://api.weixin.qq.com/card/create?access_token=";

    public static final String URL_MODIFY_STOCK = "https://api.weixin.qq.com/card/modifystock?access_token=";

    public static final String URL_DEL_COUPON = "https://api.weixin.qq.com/card/delete?access_token=";


    public UploadLogoResponse uploadLogo(String token,File logo) throws Exception{
        UploadLogoResponse response = HttpUtils.postFile(URL_UPLOAD_LOGO + token, "buffer", logo,
                                                         UploadLogoResponse.class);
        return response;
    }

     public UploadLogoResponse uploadLogo(File logo) throws Exception{
        return uploadLogo(tokenAgent.getAccessToken(),logo);
     }

    public CreateCouponResponse createCoupon(String token,Coupon coupon) throws Exception{
        HashMap param = new HashMap();
        param.put("card",coupon);
        CreateCouponResponse response = HttpUtils.postJsonHttp(URL_CREATE_COUPON+token,param,CreateCouponResponse.class);
        return response;
    }

    public CreateCouponResponse createCoupon(Coupon coupon) throws Exception{
        return createCoupon(tokenAgent.getAccessToken(),coupon);
    }

    public Response modifyStock(String cardId,long num) throws Exception{
        return modifyStock(tokenAgent.getAccessToken(),cardId,num);
    }

    public Response modifyStock(String token,String cardId,long num) throws Exception{
        StockQuantity quantity = new StockQuantity();
        quantity.setCardId(cardId);
        if (num<0){
            quantity.setDecreaseValue(-num);
        }
        else{
            quantity.setIncreaseValue(num);
        }
        Response response = HttpUtils.postJsonHttp(URL_MODIFY_STOCK+token,quantity,Response.class);
        return response;
    }

    public Response deleteCoupon(String cardId) throws Exception{
        return deleteCoupon(tokenAgent.getAccessToken(),cardId);
    }

    public Response deleteCoupon(String token,String cardId) throws Exception{
        HashMap params = new HashMap();
        params.put("card_id",cardId);
        Response response = HttpUtils.postJsonHttp(URL_DEL_COUPON+token,params,Response.class);
        return response;
    }
}
